<?php
	session_start();
	if (file_exists('php/user.php'))	require_once 'php/user.php';
	else 	if (file_exists('../user.php'))	require_once '../user.php';
	
	if (file_exists('php/photo.php'))	require_once 'php/photo.php';
	else 	if (file_exists('../photo.php'))	require_once '../photo.php';
		
	if ($_GET['op']=='onepost'){
		getOnePost($_GET['id']);
	}
	else if($_GET['op']=='npost'){
		getNPost($_GET['un'],$_GET['fr'],$_GET['ln']);
	}
	else if($_GET['op']=='ncomment'){
		getNCommentPost($_GET['id'],$_GET['fr'],$_GET['ln']);
	}
	
	function getOnePost($idpost){
		
		$hasil2 = database::query("SELECT Teks, Username_Poster FROM userpost WHERE IDPost  = '".$idpost."'");
		$baris2 = mysql_fetch_row($hasil2);
		$usr = $baris2[1];$teks = $baris2[0];
		$namafile = photo::getProfPicPath($usr);
?>
	<div class="bigminava"><img class="bigminava" src="<?php echo $namafile;?>"/></div>
	<div class="commpl">
		<div style="text-align:left;padding-left:5px;margin-bottom:8px;"><a class="unameh2" href="profile.php?<?php echo $usr;?>"><?php echo $usr;?></a></div>
		<div class="postwall"><?php echo $teks;?></div>
		<div class="postwall" style="margin-top:10px;text-align:right;"><a href="javascript:void()" onclick="show('commentinput<?php echo $idpost;?>');$('cominput<?php echo $idpost;?>').focus();" style="color:#FFF;">Add Comment</a></div>
		<br style="clear:both"/>
        <div id="listpostcomm<?php echo $idpost?>">
		<?php
		getNCommentPost($idpost,0,3);
		?>
        </div>
        <?php 
        $nlic = mysql_num_rows(user::getAllCommentPost($idpost));		
		if ($nlic>3){
		?>
		<a id="morePostComm<?php echo $idpost?>" href="javascript:void()" onClick="getNPostComment(<?php echo $idpost; ?>, 3,3)" style="color:#FFF">More Comment</a>
		<?php
		}
		/*
		$hasil3 = database::query("SELECT * FROM postkomen WHERE IDPost='$idpost'");
		while ($komenpostrow = mysql_fetch_array($hasil3)){
		?>
		<div class="boxcomment">
			<div class="minava"><img class="minava" src="<?php echo photo::getProfPicPath($komenpostrow["Username_Poster"]);?>" /></div>
			<div class="commpl">
				<div style="text-align:left;padding-left:5px;"><a class="unameh3" href="profile.php?<?php echo $komenpostrow["Username_Poster"];?>"><?php echo $komenpostrow["Username_Poster"];?></a></div>
				<div class="comment"><?php echo $komenpostrow["Teks"];?></div>
			</div>
		</div>                            
		<?php
		}*/
		?>
        
		<div id="commentinput<?php echo $idpost;?>" class="boxcomment" style="display:none;">
			<div class="minava"><?php echo'<img class="minava" src="'.photo::getProfPicPath($_SESSION['username']).'" />'?></div>
			<div class="commpl">
				<div style="text-align:left;padding-left:5px;font-size:12px;color:#333">Fill Your Comment</div>
				<div class="comment">
				<form action="php/userop.php" method="post" name="addcomment<?php echo $idpost;?>" onsubmit="addCommentPost(this,<?php echo $idpost?>,'<?php echo $username; ?>');return false;">
					<input style="width:330px;" type="text" name="cominput" id="cominput<?php echo $idpost;?>" class="cominput"  onblur="hide('commentinput<?php echo $idpost;?>');$(this.id).value='';"/>
					<input type="hidden" name="operation" value="addcomment" />
					<input type="hidden" name="idpost" value="<?php echo $idpost;?>" />
				</form>
				</div>
			</div>
		</div>
	</div>
	<br/>
	<br/><!--
	<form method="post" action = "php/delpost.php" name="form'.$idpost.'">
	<div class="button180" onclick="lanjutkan('."'form$idpost'".')">Delete</div>
	<input type="hidden" name="sembunyi" value="'.$idpost.'"/>
	</form>
	</div>-->
<?php
	}
	
	function getNPost($username,$from,$length){
		$hasil = user::getAllPost($username);//database::query("SELECT IDPost FROM posttag WHERE username = '".$username."' ORDER BY IDPost DESC");
		if (mysql_num_rows($hasil)>0)
		{
			$nlist = mysql_num_rows($hasil);
			if ($nlist!=0)	mysql_data_seek($hasil,$from);
			$count=0;
			while (($baris = mysql_fetch_row($hasil))&&$count<$length)
			{	$idpost = $baris[0];
				echo '<div id="post'.$idpost.'" class="boxpostwall">';
				getOnePost($idpost);
				echo '</div>';
				$count++;
			}
			
			if ($nlist <= $from+$length){
				echo '<input type="hidden" id="posthbs" value="1"/>';
			}
		}
	}
	
	function getNCommentPost($idpost,$from,$length){
		$hasil3 = user::getAllCommentPost($idpost);
		
		$nlist = mysql_num_rows($hasil3);
		if ($nlist!=0)	mysql_data_seek($hasil3,$from);
		$count=0;
		while (($komenpostrow = mysql_fetch_array($hasil3))&&$count<$length){
		?>
			<div class="boxcomment" style="min-height:40px;">
				<div class="minava" style="height:40px;width:40px;"><img class="minava" src="<?php echo photo::getProfPicPath($komenpostrow["Username_Poster"]);?>" /></div>
				<div class="commpl">
					<div style="text-align:left;padding-left:5px;"><a class="unameh3" href="profile.php?<?php echo $komenpostrow["Username_Poster"];?>"><?php echo $komenpostrow["Username_Poster"];?></a></div>
					<div class="comment"><?php echo $komenpostrow["Teks"];?></div>
				</div>
			</div>                            
		<?php
			$count++;
		}	
		
		if ($nlist <= $from+$length){
			echo '<input type="hidden" id="postcmthbs'.$idpost.'" value="1"/>';
		}
	}
?>